package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: csmall-product
 * @description:
 * @author: Mr.Tang
 * @create: 2023-03-29 17:25
 **/

@Slf4j
@RestController
@RequestMapping("categories")
@Api(tags = "03. 类别管理模块")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    //http://localhost:10086/categories/add-new

    @PostMapping("add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CategoryAddNewDTO categoryAddNewDTO) {
        log.debug("开始处理【添加类别】的请求，参数：{}",categoryAddNewDTO);
        categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:10086/categories/list-by-parent?parentId=0
    @GetMapping("/list-by-parent")
    @ApiOperation("根据父级查询子级类别列表")
    @ApiOperationSupport(order = 420)
    public JsonResult listByParentId(Long parentId) {
        List<CategoryListItemVO> list = categoryService.listByParentId(parentId);
        return JsonResult.ok(list);
    }
}
